/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.com.cennavi.wordcount;

import scala.Tuple2;

import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

public final class JavaWordCount {
	private static final Pattern SPACE = Pattern.compile(" ");

	public static void main(String[] args) throws Exception {

		if (args.length < 3) {
			System.out.println("Arguments is error:" + args.toString());
			return;
		}
		System.out.println("master："+args[0]);
		System.out.println("jarsPath："+args[1]);
		System.out.println("输入参数路径："+args[2]);
//		System.out.println("outputPath："+args[3]);
//		System.out.println("tableName："+args[4]);
		/*
		 * standalone:spark://master46:7077 yarn:yarn-client
		 */
		String master = args[0];
		// /home/hadoop/testspark/lib/BasicTrajectoryAnalysisBySpark-0.0.1-SNAPSHOT.jar
		String jarsPath = args[1];
		/*
		 * HA:"hdfs://nujhadoop/BasicTrajectory/BAIDU/2015/m=06/d=01/
		 * BASIS_TRAJECTORY_2015_*
		 * 直接连接:"hdfs://master56:8020/BasicTrajectory/BAIDU/2015/m=06/d=01/
		 * BASIS_TRAJECTORY_2015_*
		 */
		String inputPath = args[2];
//		String outputPath = args[3];
//		String tableName = args[4];

		JavaSparkContext ctx = SparkUtils.getSparkContext("wordCountTest_20160226", master, jarsPath);
		
		System.out.println("输入参数路径："+args[2]);
		
		JavaRDD<String> lines = ctx.textFile(args[2], 1);

		JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
			/**
			 * @Fields serialVersionUID : TODO（用一句话描述这个变量表示什么）
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Iterator<String> call(String t) throws Exception {
				// TODO Auto-generated method stub
				return null;
			}

//			@Override
//			public Iterable<String> call(String s) {
//				return Arrays.asList(SPACE.split(s));
//			}
		});

		JavaPairRDD<String, Integer> ones = words.mapToPair(new PairFunction<String, String, Integer>() {

			/**
			 * @Fields serialVersionUID : TODO（用一句话描述这个变量表示什么）
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Tuple2<String, Integer> call(String s) {
				return new Tuple2<String, Integer>(s, 1);
			}
		});

		JavaPairRDD<String, Integer> counts = ones.reduceByKey(new Function2<Integer, Integer, Integer>() {

			/**
			 * @Fields serialVersionUID : TODO（用一句话描述这个变量表示什么）
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Integer call(Integer i1, Integer i2) {
				return i1 + i2;
			}
		});

		List<Tuple2<String, Integer>> output = counts.collect();
		for (Tuple2<?, ?> tuple : output) {
			System.out.println(tuple._1() + ": " + tuple._2());
		}
		ctx.stop();
	}
}
